// index.js
import Toast from "../../ui/toast/index"
// 获取应用实例
const app = getApp()

Page({
  data: {
    banner_list:[],
    pic_url:app.base_url,
    nav_list:[],
    xs_list:[],//限时购
    list:[],
    home_active:true,
    yhq_list:[],//优惠券
    new_user_list:[],//新人购
    page:{
      page:1,
    },
    is_loading:false,//加载
    car_goods_num:0,//购物车商品数量
    yhq_list:[],
    yhq_popshow:false,
  },

  //获取轮播图
  get_ad_list(){
    app.http.request("index.php?r=api/index/adlist",{}).then((res)=>{
  
      if(res.data.result){
        for(let i=0;i<res.data.data.length;i++){
          this.data.banner_list.push(res.data.data[i].thumb_pic);
        }
        this.setData({
          banner_list:this.data.banner_list,
        });
      }
    });
  },
  
  //跳转内容
  to_content(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/goods/content/index?id='+id,
    })
  },

  //跳转分类
  to_cate(){
    wx.navigateTo({
      url: '/cate/index/index',
    })
  },

  //跳转购物车
  to_car(){
    wx.navigateTo({
      url: '/car/index/index',
    })
  },

  //跳转我的
  to_mine(){
    wx.navigateTo({
      url: '/mine/index/index',
    })
  },

  //获取优惠券
  get_yhq(){
    app.http.request("index.php?r=api/index/yhq").then((res)=>{
      if(res.data.result){
        this.setData({
          yhq_list:res.data.data,
        });
      }
    });
  },

  //获取新人购
  get_new_goods(){
    app.http.request("index.php?r=api/index/newusergoods").then((res)=>{
      if(res.data.result){
        this.setData({
          new_user_list:res.data.data,
        });
      }
    });
  },

  //获取首页导航
  get_nav_list(){
    app.http.request("index.php?r=api/index/navlist").then((res)=>{
      if(res.data.result){
        this.setData({
          nav_list:res.data.data,
        });
      }
    });
  },

  //限时购
  get_xs_goods(){
    app.http.request("index.php?r=api/index/xsgoods").then((res)=>{
      if(res.data.result){
        this.setData({
          xs_list:res.data.data,
        });
      }
    });
  },

  //获取商品列表
  get_list(){
    this.setData({
      is_loading:true,
    });
    app.http.request("index.php?r=api/index/goods",{page:this.data.page}).then((res)=>{
      if(res.data.result){
        this.data.list = this.data.list.concat(res.data.data);
        this.setData({
          list:this.data.list,
        });
        this.data.page.page++;
      }else{
        Toast({
          context:this,
          selector:"#toast",
          message:"没有内容了",
        });
      }
      this.setData({
        is_loading:false,
      });
    });
  },

   //更新购物车数量
   refresh_car(){
    let goods = wx.getStorageSync('goods');
    let num=0;
    if(goods){
        num=goods.length;
    }
    this.setData({
        car_goods_num:num,
    });
},

 //显示优惠券
 change_yhq_pop(){
  this.setData({
      yhq_popshow:true,
  });
  this.get_yhq_list();
},

//获取优惠券
get_yhq_list(){
  app.http.request("index.php?r=api/content/yhq",{}).then((res)=>{
      if(res.data.result==1){
          this.setData({
              yhq_list:res.data.data,
          });
      }
  });
},

//领取优惠券
click_yhq(e){
  let id = e.currentTarget.dataset.id;
  app.login.request("index.php?r=api/content/getyhq",{id:id}).then((res)=>{
      if(res.data.result){
          Toast({
              context:this,
              selector:"#toast",
              message:"领取成功"
          });
          this.get_yhq_list();
      }else{
          Toast({
              context:this,
              selector:"#toast",
              message:res.data.data,
          });
      }
  });
},

close_yhq_pop(){
  this.setData({
      yhq_popshow:false,
  });
},

//跳转列表
to_goods_list(e){
  let id = e.currentTarget.dataset.id;
  wx.navigateTo({
    url: '/goods/list/list?id='+id,
  })
},

  //滚动事件
  onScroll(){

  },

  //下拉刷新
  onRefresh(){
    this.get_list();
  },

  

  onLoad() {
    this.get_yhq();
    this.get_new_goods();
    this.get_nav_list();
    this.get_xs_goods();
    this.get_list();
    
    this.get_ad_list();
    app.get_name();
  },

   /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      this.get_list();
    },

    onShow(){
      this.refresh_car();
    }
  
  
})
